package com.miguo.services;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;

import com.miguo.activity.FavorActivity;
import com.miguo.db.GoodsDB;
import com.miguo.framework.utils.Log;
import com.miguo.ui.R;
import com.miguo.utils.CommonUtils;

public class NotiService extends Service {

    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onCreate() {
        super.onCreate();
    }


    public int onStartCommand(Intent intent, int flags, int startId) {
        handleCommand(intent, startId);
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null)
                return START_STICKY;
        }
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void handleCommand(Intent intent, int startId) {
        if (intent != null) {
            Context context = getApplicationContext();
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                Long begin_time = bundle.getLong("begin_time");
                String contentText;
                StringBuilder stringBuilder = new StringBuilder("您有");
                stringBuilder.append(
                        GoodsDB.getInstance(context).getNotiCountByStartTime(
                                begin_time)).append("个宝贝将要在5分钟后开卖");
                contentText = stringBuilder.toString();
                Log.e("localNoti noti success!");
                setNoti(context,
                        CommonUtils.getString(context, R.string.app_name),
                        contentText);
                return;
            }
            Log.e("localNoti noti failed!");
        }
    }

    private void setNoti(Context context, CharSequence contentTitle,
                         CharSequence contentText) {
//        int time = (int) System.currentTimeMillis();
//        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//        Notification notification = new Notification(R.drawable.noti_icon,
//                CommonUtils.getString(context, R.string.app_name) + "开抢提醒",
//                System.currentTimeMillis());
//        notification.flags |= Notification.FLAG_AUTO_CANCEL;
//        notification.defaults = Notification.DEFAULT_SOUND
//                | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE;
//        notification.ledARGB = Color.BLUE;// led灯颜色
//        notification.ledOffMS = 100;// 关闭时间 毫秒
//        notification.ledOnMS = 100;// 开启时间 毫秒
//        Intent notificationIntent = new Intent(this, FavorActivity.class);
//        notificationIntent.putExtra("favor", false);
//        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
//                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
//        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
//                notificationIntent, 0);
//        notification.setLatestEventInfo(context, contentTitle, contentText,
//                contentIntent);
//        mNotificationManager.notify(time, notification);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
        mBuilder.setAutoCancel(true)
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setDefaults(Notification.DEFAULT_ALL)
                .setOngoing(false)
                .setSmallIcon(R.drawable.noti_icon)
                .setContentTitle(contentTitle)
                .setContentText(contentText)
                .setTicker(CommonUtils.getString(context, R.string.app_name) + "开抢提醒");
        Intent intent = new Intent(context, FavorActivity.class);
        intent.putExtra("favor", false);
        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        int requestCode = (int) System.currentTimeMillis();
        PendingIntent pendingIntent = PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(pendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(requestCode, mBuilder.build());
    }

}
